package com.hr.mes.controller;

import com.hr.mes.dto.Page;
import com.hr.mes.dto.Result;
import com.hr.mes.entity.Menu;
import com.hr.mes.enums.MyExceptionEnum;
import com.hr.mes.exception.MyException;
import com.hr.mes.service.MenuService;
import com.hr.mes.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiresRoles("admin")
@Api(tags = "菜单管理")
@CrossOrigin
@RequestMapping("/api/menu")
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("新增菜单")
    @PostMapping("addMenu")
    public Result addMenu(@RequestBody Menu menu){
        try{
            menuService.addMenu(menu);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_ADD_ERROR, e);
        }
    }

    @ApiOperation("删除菜单")
    @GetMapping("deleteMenu")
    public Result deleteMenu(@RequestParam Integer menuId){
        try{
            menuService.deleteMenu(menuId);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_DELETE_ERROR, e);
        }
    }

    @ApiOperation("更新菜单")
    @PostMapping("updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        try{
            menuService.updateMenu(menu);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_UPDATE_ERROR, e);
        }
    }

    @ApiOperation("查询菜单")
    @GetMapping("getMenu")
    public Result getMenu(@RequestParam Integer menuId){
        try{
            Menu menu = menuService.getMenu(menuId);
            return Result.ok(menu);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_VIEW_ERROR, e);
        }
    }

    @ApiOperation("查询菜单列表")
    @GetMapping("getMenuList")
    public Result getMenuList(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        try{
            Page<Menu> menuList = menuService.getMenuList(pageNo, pageSize);
            return Result.ok(menuList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_VIEW_ERROR, e);
        }
    }

    @ApiOperation("查询可用的菜单列表")
    @GetMapping("getAvailableMenuList")
    public Result getAvailableMenuList(){
        try{
            List<MenuVO> menuList = menuService.getAvailableMenuList();
            return Result.ok(menuList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_VIEW_ERROR, e);
        }
    }

    @ApiOperation("查询子菜单")
    @GetMapping("getChildren")
    public Result getChildren(@RequestParam Integer menuId){
        try{
            List<Menu> menuList = menuService.getChildren(menuId);
            return Result.ok(menuList);
        }catch (Exception e){
            throw new MyException(MyExceptionEnum.MENU_VIEW_ERROR, e);
        }
    }

}
